﻿/*
Created By:     lixueming
Created When:   2012-3-31
Created Whys:   UKnow 2  MyProfile
*/


//var g_langID = 1033;
var MyCommnetimgSrc = "/uploadfiles/image/";

//$(document).ready(function () {
//    GetMyAccount();
//    MyInterests();
//    MyProfile();
//    GetProvice();
//    showbirday();


//});


var showbirday=function() 
{
    new DateSelector("year", "month", "day", { MinYear: 50, MaxYear: -10 });
}
var cancelmember = g_langID == 1033 ? "Do you CancelMembership?" : "确认注销会员?";
$(function () {
    $('#Provicelist').change(function () {
        GetCity();
    });
    $('.editinfo h1 a').click(function () {
        if (confirm(cancelmember)) {
            $.colorbox({
                href: "/"+g_siteroot+"/cancelmembership.html",
                width: 428,
                height: 283
            });
        }
    });
 
});



function showheadimg() {
    var editor = new imageEditorDialog(g_langID);
    editor.show(520, 520);

}
function fillImage(src, fileName) {
    $("#head_bigimg").attr("src", src + ".jpg");
    $("#head_midimg").attr("src", src + ".jpg");
    $("#head_smaimg").attr("src", src + ".jpg");
    $("#hid_image").attr("value", fileName+".jpg");

}

var MyInterests = function () {
    $.ajax({
        type: "GET",
        url: "/GateService/MyAccountAjax.aspx",
        dataType: "json",
        data: "cmd=GetMyInterests&langID=" + g_langID + "&random=" + new Date().toLocaleString(),
        beforeSend: function(XMLHttpRequest) {
        },
        success: function(data, textStatus) {
            $('#Dynamic_MyInterestContent').html("");
            var html = $('#Dynamic_MyInterestContent');
            var s = "";
            var prefix = "MyInterests_";
            $.each(data.Table, function(index, row) {
                var fromid = IsNull(row.COL_FORM_ID);
                var COL_FORM_NAME = IsNull(row.COL_FORM_NAME);
                var isSelected = IsNull(row.IsSelected);
                var check = parseInt(isSelected) > 0 ? "checked" : "";
                s += " <li><input name=\"MyInterests_" + fromid + "\" type=\"checkbox\" " + check + " value="+fromid+" > " + COL_FORM_NAME + "</li>";

            });

            html.append(s);
        },
        complete: function(XMLHttpRequest, textStatus) {

        },
        error: function() {
           
        }
    });
};
var MyProfile = function () {
    $.ajax({
        type: "GET",
        url: "/GateService/MyAccountAjax.aspx",
        dataType: "json",
        data: "cmd=GetMyProfile&langID=" + g_langID + "&random=" + new Date().toLocaleString(),
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (data, textStatus) {
            if (data != null) {
                var MemberImg = IsNull(data.MemberImg);
                var email = IsNull(data.Email);
                var Name = IsNull(data.Name);
                //var Name = IsNull(data.AccountName);
                var Mobile = IsNull(data.Mobile);
                var MOBLIEISME = IsNull(data.MOBLIEISME);
                var Birthday = IsNull(data.Birthday);
                var BIRTHDAYISME = IsNull(data.BIRTHDAYISME);
                var City = IsNull(data.City);
                var Provice = IsNull(data.Provice);
                var artist = IsNull(data.FavoriteArtist);
                var StudyAbroad = IsNull(data.StudyAbroad);
                var IELTS = IsNull(data.IELTS);
                var ArtsAndCulture = IsNull(data.ArtsAndCulture);
                var showimg = MemberImg != "" ? Environment.FilePath.UploadFrontImagePath + "/" + MemberImg : globals.header_def;
                var CheckPw = data.CheckPw;
                var membername = IsNull(data.Name);
                //alert(membername);
                //alert(CheckPw);
                if (CheckPw == true) {
                    $("#checkoldpw").attr("value", 1);
                    $(".changepwd").hide();
                } else {
                    //alert('dddddd');
                    $("#Isdisplay").hide();
                    $("#chk_pass").attr({ disabled: "disabled" });
                    $(".changepwd").show();
                }

                $("#hid_image").attr("value", MemberImg);
              
               
                $("#head_bigimg").attr("src", showimg);
                //$("#head_bigimg").attr("error", "this.src=" + globals.header_def + "");
               // .attr("onerror","this.src="+aa+"");

                $("#head_midimg").attr("src", showimg);
                $("#head_smaimg").attr("src", showimg);

                if (email != "") {
                    $("#txt_email").attr("value", email);
                    $('#txt_email').attr("readonly", true);
                    $("#emailtext").html(email);
                    $('#txt_email').hide();

                } else {
                    $("#emailtext").html("").hide();
                }
                $("#txt_username").attr("value", Name);
                $("#txt_mobile").attr("value", Mobile);

                if (MOBLIEISME != "" && MOBLIEISME == true) {
                    $("#chk_mobile").attr("checked", true);
                    // $("#chk_mobile").attr("disabled", true);
                }
                if (BIRTHDAYISME != "" && BIRTHDAYISME == true) {
                    $("#chk_birth").attr("checked", true);

                }
                $("#txt_artist").attr("value", artist);
                if (StudyAbroad != "" && StudyAbroad == 1) {
                    $("#chk_StudyAbroad").attr("checked", true);
                }
                //                if (IELTS != "" && IELTS == 1) {
                //                    $("#chk_IELTS").attr("checked", true);
                //                }
                //                if (ArtsAndCulture != "" && ArtsAndCulture == 1) {
                //                    $("#chk_ArtsAndCulture").attr("checked", true);
                //                }
                if (Birthday != "") {
                    var bir = Birthday.split("/");
                    var year = bir[0];
                    var month = bir[1];
                    var day = bir[2];
                    $("#year").attr("value", year);
                    $("#month").attr("value", month);
                    $("#day").attr("value", day);

                }
                //                if (City != "") {
                //                    //$("#Citylist").attr("value", City);
                //                    $("#cityid").attr("value", City);

                //                }
                //                if (Provice != "") {

                //                    $("#Provicelist").attr("value", Provice);
                //                }
                $("#txtmembername").attr("value", membername);

                GetProvice(Provice, City);

            }
        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function () {
            alert("errora");
        }
    })
};

$(function () {
    $('#head_bigimg').error(function () {
        this.src = globals.header_def;

    });
    $("#head_midimg").error(function () {
        this.src = globals.header_def;

    });
    $("#head_smaimg").error(function () {
        this.src = globals.header_def;

    });
   
     
});





var MyProValidate = function () {
    var email = $('#txt_email').val();
    var username = $('#txt_username').val();
    var pwold = $('#txtoldpw').val();
    var newpw = $('#txtnewpw').val();
    var newpw1 = $('#txtnewpw1').val();
    var mobile = $("#txt_mobile").val();
    if (email == "") {
        alert(MyProfileValidate.emailNull);
        return false;
    }
    var existemail = $("#emailexist").val();
    if (typeof (existemail) == "undefined" || parseInt(existemail) == 1) {
        alert(MyProfileValidate.emailexist);
        return false;
    }
    if (!CheckEmail()) {
        return false;
    }
    if (username == "") {
        alert(MyProfileValidate.usernameNull);
        return false;
    }
    if (!CheckUsername()) {
        return false;
    }
    if (mobile != MyProfileValidate.inputphone) {
        if (mobile != "") {
            if ( /^[0-9]+$/ .test($.trim(mobile)) & ($.trim(mobile).length == 11)) {

            } else {
                alert(MyProfileValidate.phonenum);
                return false;
            }
        }
    }
    var chk = $("#chk_pass");
    //alert(chk.attr("checked"));
    //alert(chk.attr("disabled"));
    if (chk.attr("checked") || chk.attr("disabled") == "disabled") {
        if (!checkpw()) {
            return false;
        }

        if (newpw == "") {
            alert(MyProfileValidate.newpwNull);
            return false;
        }
        if (newpw.length >= 6 && newpw.length <= 18) {
        } else {
            alert(MyProfileValidate.pwlength);
        }
        var reg = "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$";
        if (!new RegExp(reg).test(newpw)) {
            alert(MyProfileValidate.pwformat);
            return false;
        }

        if (newpw1 == "") {
            alert(MyProfileValidate.newpwconNull);
            return false;
        }
        //        if ($.trim(newpw1).length < 5) {
        //            alert(MyProfileValidate.pwlength);
        //        }
        if (newpw != newpw1) {
            alert(MyProfileValidate.pwcoflct);
            return false;
        }
    }
    return true;

}

$(function () {
    $('#txt_email').focus(function () {
        if (this.value == MyProfileValidate.inputemail) {
            this.value = "";
            return;
        }

    });
    $('#txt_email').blur(function () {
//        if (this.value == MyProfileValidate.inputemail) {
//            this.value = "";
//            return;
//        }
        if (this.value == "") {
            alert(MyProfileValidate.emailNull);
            return;
        }
        if (this.readonly != true) {
            CheckEmail();
            return;
        }

    });
    $('#txt_username').focus(function () {
        if (this.value == MyProfileValidate.inputusername) {
            this.value = "";
            return;
        }

    });
    $('#txt_username').blur(function () {

        if (this.value == "") {
            alert(MyProfileValidate.usernameNull);

        }
        if (!CheckUsername()) {
            return false;
        }
    });


    // {
    $('#txtnewpw').blur(function () {
        var chk = $("#chk_pass");
        if (chk.attr("checked") || chk.attr("disabled") == "disabled") {
            if (this.value == "") {
                alert(MyProfileValidate.newpwNull);
                return;
            }
            if ($.trim(this.value).length < 5) {
                alert(MyProfileValidate.pwlength);
                return;
            }
        }
    });
    $('#txtnewpw1').blur(function () {
        var chk = $("#chk_pass");
        if (chk.attr("checked") || chk.attr("disabled") == "disabled") {
            if (this.value == "") {
                alert(MyProfileValidate.newpwconNull);
                return;
            }
            if (this.value != $('#txtnewpw').val()) {
                alert(MyProfileValidate.pwcoflct);
                return;
            }
        }
    });
    // }
    $('#txt_mobile').focus(function () {
        if (this.value == MyProfileValidate.inputphone) {
            this.value = "";
        }

    });

});

var checkpw = function () {
    var check = $("#checkoldpw").val();
    if (parseInt(check) == 1) {
        var pwstr = $("#txtoldpw").val();
        if (pwstr == "") {
            alert(MyProfileValidate.oldpwNull);
            return false;
        }
    }
    return true;
};

$(function () {
    $("#form_myprofile").submit(function (e) {
        if (!MyProValidate()) {
            return;
        }
        e.preventDefault();
        var selectedChk = "";
        var modeifypw = "";
        var chk = $("#chk_pass");
        if (chk.attr("checked") || chk.attr("disabled") == "disabled") {
            modeifypw = "&modifypw=1&";
        }
        $("ol#Dynamic_MyInterestContent :checkbox:checked").each(function () {
            selectedChk += $(this).val() + ",";
        });

        var dataString = $("#form_myprofile").serialize();
        // alert(dataString);
        $.ajax({
            type: "POST",
            url: "/GateService/SaveMyProfile.aspx",
            data: modeifypw + dataString + "&selInterest=" + selectedChk.trim() + "&langID=" + g_langID,
            dataType: "json",
            success: function (data) {
                //alert('ggggg');
                if (data.Result) {
                    if (g_langID == 2052) {
                        alert(data.Message);
                    } else {
                        alert(globals.myprofilesucc);
                    }
                    location.reload();
                } else {
                    alert(data.Message);
                }


            }

        });

    });
});


var CheckEmail = function () {
    var email = $("#txt_email").val();
    var emailreg = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    if (!new RegExp(emailreg).test(email)) {
        alert(MyProfileValidate.emailerror);
        return false;
    }
    $.ajax({
        type: "GET",
        url: "/GateService/MyAccountAjax.aspx",
        dataType: "json",
        data: "cmd=CheckEmail&email=" + email + "&langID=" + g_langID + "&random=" + new Date().toLocaleString(),
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (data, textStatus) {
            if (data.Result == true) {
                $("#emailexist").attr("value", "0");
                return true;
            } else {
                $("#emailexist").attr("value", "1");
                alert(MyProfileValidate.emailexist);
                return false;
            }
        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function () {
            alert("error");
        }
    });
    return true;

};

var CheckUsername = function () {
    var username = $('#txt_username').val();
    var usernamereg = "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$";
    if (!new RegExp(usernamereg).test(username)) {
        alert(MyProfileValidate.username);
        return false;
    }
    $.ajax({
        type: "GET",
        url: "/GateService/MyAccountAjax.aspx",
        dataType: "json",
        data: "cmd=CheckUsername&username=" + username + "&langID=" + g_langID + "&random=" + new Date().toLocaleString(),
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (data, textStatus) {
            if (data.Result == true) {
                //$("#emailexist").attr("value", "0");
                return true;
            } else {
                alert(MyProfileValidate.userexist);
                return false;
            }
        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function () {
            alert("error");
        }
    });
    return true;

};


var CancelMember = function () {
    $.ajax({
        type: "GET",
        url: "/GateService/MyAccountAjax.aspx",
        dataType: "json",
        data: "cmd=CancleMemberShip&langID=" + g_langID + "&random=" + new Date().toLocaleString(),
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (data, textStatus) {
            if (data.Result == true) {
                //alert(data.Result);
                //跳转到首页
                window.location.href = "/" + g_siteroot + "/home.html";

            }
        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function () {

        }
    });
};

var check_pass = function () {
    var chk = $("#chk_pass");
    if (chk.attr("checked")) {
        $(".changepwd").show();
    } else {
        $(".changepwd").hide();
    }
}

   

